home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / clang / cweb31.zip / EXAMPLES / WMERGE.W < prev    next >
Text File  |  1993-06-10  |  25KB  |  710 lines

  1. \def\9#1{} % this hack is explained in CWEB manual Appendix F11
  2.  
  3. @* Introduction.  This file contains the program \.{wmerge},
  4. which takes two or more files and merges them according
  5. to the conventions of \.{CWEB}. Namely, it takes an ordinary \.{.w}
  6. file and and optional \.{.ch} file and sends the corresponding
  7. \.{.w}-style file to standard output (or to a named file), 
  8. expanding all ``includes''
  9. that might be specified by \.{@@i} in the original \.{.w} file.
  10. (A more precise description appears in the section on ``command line
  11. arguments'' below.)
  12.  
  13. @c
  14. #include <stdio.h>
  15. #include <stdlib.h> /* declaration of |getenv| */
  16. #include <ctype.h> /* definition of |isalpha|, |isdigit| and so on */
  17. @<Definitions@>@;
  18. @<Predeclarations of functions@>@;
  19. @<Functions@>@;
  20. main (ac,av)
  21. int ac; char **av;
  22. {
  23.   argc=ac; argv=av;
  24.   @<Set the default options@>;
  25.   @<Scan arguments and open output file@>;
  26.   reset_input();
  27.   while (get_line())
  28.     put_line();
  29.   fflush(out_file);
  30.   check_complete();
  31.   fflush(out_file);
  32.   return wrap_up();
  33. }
  34.  
  35. @ @<Definitions@>=
  36. typedef short boolean;
  37. typedef unsigned char eight_bits;
  38. typedef char ASCII; /* type of characters inside \.{WEB} */
  39.  
  40. @ We predeclare some standard string-handling functions here instead of
  41. including their system header files, because the names of the header files
  42. are not as standard as the names of the functions. (There's confusion
  43. between \.{<string.h>} and \.{<strings.h>}.)
  44.  
  45. @<Predecl...@>=
  46. extern int strlen(); /* length of string */
  47. extern char* strcpy(); /* copy one string to another */
  48. extern int strncmp(); /* compare up to $n$ string characters */
  49. extern char* strncpy(); /* copy up to $n$ string characters */
  50.  
  51. @ @<Predec...@>=
  52.  
  53. @ The lowest level of input to the \.{WEB} programs
  54. is performed by |input_ln|, which must be told which file to read from.
  55. The return value of |input_ln| is 1 if the read is successful and 0 if
  56. not (generally this means the file has ended).
  57. The characters of the next line of the file
  58. are copied into the |buffer| array,
  59. and the global variable |limit| is set to the first unoccupied position.
  60. Trailing blanks are ignored. The value of |limit| must be strictly less
  61. than |buf_size|, so that |buffer[buf_size-1]| is never filled.
  62.  
  63. Some of the routines use the fact that it is safe to refer to
  64. |*(limit+2)| without overstepping the bounds of the array.
  65.  
  66. @d buf_size 100
  67.  
  68. @<Definitions...@>=
  69. ASCII buffer[buf_size]; /* where each line of input goes */
  70. ASCII *buffer_end=buffer+buf_size-2; /* end of |buffer| */
  71. ASCII *limit; /* points to the last character in the buffer */
  72. ASCII *loc; /* points to the next character to be read from the buffer */
  73.  
  74. @ In the unlikely event that your standard I/O library does not
  75. support |feof|, |getc| and |ungetc|, you may have to change things here.
  76. @^system dependencies@>
  77.  
  78. Incidentally, here's a curious fact about \.{CWEB} for those of you
  79. who are reading this file as an example of \.{CWEB} programming.
  80. The file \.{stdio.h} includes a typedef for
  81. the identifier |FILE|, which is not, strictly speaking, part of \CEE/.
  82. It turns out \.{CWEAVE} knows that |FILE| is a reserved word (after all,
  83. |FILE| is almost as common as |int|); indeed, \.{CWEAVE} knows all
  84. the types of the ISO standard \CEE/ library. But
  85. if you're using other types like {\bf caddr\_t},
  86. @:caddr_t}{\bf caddr_t@>
  87. which is defined in \.{/usr/include/sys/types.h}, you should let
  88. \.{WEAVE} know that this is a type, either by including the \.{.h} file
  89. at \.{WEB} time (saying \.{@@i /usr/include/sys/types.h}), or by
  90. using \.{WEB}'s format command (saying \.{@@f caddr\_t int}).  Either of
  91. these will make {\bf caddr\_t} be treated in the same way as |int|. 
  92.  
  93. @<Func...@>=
  94. input_ln(fp) /* copies a line into |buffer| or returns 0 */
  95. FILE *fp; /* what file to read from */
  96. {
  97.   register int  c=EOF; /* character read; initialized so some compilers won't complain */
  98.   register char *k;  /* where next character goes */
  99.   if (feof(fp)) return(0);  /* we have hit end-of-file */
  100.   limit = k = buffer;  /* beginning of buffer */
  101.   while (k<=buffer_end && (c=getc(fp)) != EOF && c!='\n')
  102.     if ((*(k++) = c) != ' ') limit = k;
  103.   if (k>buffer_end)
  104.     if ((c=getc(fp))!=EOF && c!='\n') {
  105.       ungetc(c,fp); loc=buffer; err_print("! Input line too long");
  106. @.Input line too long@>
  107.   }
  108.   if (c==EOF && limit==buffer) return(0);  /* there was nothing after
  109.     the last newline */
  110.   return(1);
  111. }
  112.  
  113. @ Now comes the problem of deciding which file to read from next.
  114. Recall that the actual text that \.{CWEB} should process comes from two
  115. streams: a |web_file|, which can contain possibly nested include
  116. commands \.{@@i}, and a |change_file|, which might also contain
  117. includes.  The |web_file| together with the currently open include
  118. files form a stack |file|, whose names are stored in a parallel stack
  119. |file_name|.  The boolean |changing| tells whether or not we're reading
  120. from the |change_file|.
  121.  
  122. The line number of each open file is also kept for error reporting.
  123.  
  124. @f line x /* make |line| an unreserved word */
  125. @d max_include_depth 10 /* maximum number of source files open
  126.   simultaneously, not counting the change file */
  127. @d max_file_name_length 60
  128. @d cur_file file[include_depth] /* current file */
  129. @d cur_file_name file_name[include_depth] /* current file name */
  130. @d cur_line line[include_depth] /* number of current line in current file */
  131. @d web_file file[0] /* main source file */
  132. @d web_file_name file_name[0] /* main source file name */
  133.  
  134. @<Definitions...@>=
  135. int include_depth; /* current level of nesting */
  136. FILE *file[max_include_depth]; /* stack of non-change files */
  137. FILE *change_file; /* change file */
  138. char file_name[max_include_depth][max_file_name_length];
  139.   /* stack of non-change file names */
  140. char change_file_name[max_file_name_length]; /* name of change file */
  141. char alt_web_file_name[max_file_name_length]; /* alternate name to try */
  142. int line[max_include_depth]; /* number of current line in the stacked files */
  143. int change_line; /* number of current line in change file */
  144. int change_depth; /* where \.{@@y} originated during a change */
  145. boolean input_has_ended; /* if there is no more input */
  146. boolean changing; /* if the current line is from |change_file| */
  147. boolean web_file_open=0; /* if the web file is being read */
  148.  
  149. @ When |changing=0|, the next line of |change_file| is kept in
  150. |change_buffer|, for purposes of comparison with the next
  151. line of |cur_file|. After the change file has been completely input, we
  152. set |change_limit=change_buffer|,
  153. so that no further matches will be made.
  154.  
  155. Here's a shorthand expression for inequality between the two lines:
  156.  
  157. @d lines_dont_match (change_limit-change_buffer != limit-buffer ||
  158.   strncmp(buffer, change_buffer, limit-buffer))
  159.  
  160. @<Def...@>=
  161. char change_buffer[buf_size]; /* next line of |change_file| */
  162. char *change_limit; /* points to the last character in |change_buffer| */
  163.  
  164. @ Procedure |prime_the_change_buffer| sets |change_buffer| in
  165. preparation for the next matching operation. Since blank lines in the change
  166. file are not used for matching, we have
  167. |(change_limit==change_buffer && !changing)| if and only if
  168. the change file is exhausted. This procedure is called only when
  169. |changing| is 1; hence error messages will be reported correctly.
  170.  
  171. @<Func...@>=
  172. void
  173. prime_the_change_buffer()
  174. {
  175.   change_limit=change_buffer; /* this value is used if the change file ends */
  176.   @<Skip over comment lines in the change file; |return| if end of file@>;
  177.   @<Skip to the next nonblank line; |return| if end of file@>;
  178.   @<Move |buffer| and |limit| to |change_buffer| and |change_limit|@>;
  179. }
  180.  
  181. @ While looking for a line that begins with \.{@@x} in the change file, we
  182. allow lines that begin with \.{@@}, as long as they don't begin with \.{@@y},
  183. \.{@@z} or \.{@@i} (which would probably mean that the change file is fouled up).
  184.  
  185. @<Skip over comment lines in the change file...@>=
  186. while(1) {
  187.   change_line++;
  188.   if (!input_ln(change_file)) return;
  189.   if (limit<buffer+2) continue;
  190.   if (buffer[0]!='@@') continue;
  191.   if (isupper(buffer[1])) buffer[1]=tolower(buffer[1]);
  192.   if (buffer[1]=='x') break;
  193.   if (buffer[1]=='y' || buffer[1]=='z' || buffer[1]=='i') {
  194.     loc=buffer+2;
  195.     err_print("! Missing @@x in change file");
  196. @.Missing @@x...@>
  197.   }
  198. }
  199.  
  200. @ Here we are looking at lines following the \.{@@x}.
  201.  
  202. @<Skip to the next nonblank line...@>=
  203. do {
  204.   change_line++;
  205.   if (!input_ln(change_file)) {
  206.     err_print("! Change file ended after @@x");
  207. @.Change file ended...@>
  208.     return;
  209.   }
  210. } while (limit==buffer);
  211.  
  212. @ @<Move |buffer| and |limit| to |change_buffer| and |change_limit|@>=
  213. {
  214.   change_limit=change_buffer-buffer+limit;
  215.   strncpy(change_buffer,buffer,limit-buffer+1);
  216. }
  217.  
  218. @ The following procedure is used to see if the next change entry should
  219. go into effect; it is called only when |changing| is 0.
  220. The idea is to test whether or not the current
  221. contents of |buffer| matches the current contents of |change_buffer|.
  222. If not, there's nothing more to do; but if so, a change is called for:
  223. All of the text down to the \.{@@y} is supposed to match. An error
  224. message is issued if any discrepancy is found. Then the procedure
  225. prepares to read the next line from |change_file|.
  226.  
  227. This procedure is called only when |buffer<limit|, i.e., when the
  228. current line is nonempty.
  229.  
  230. @<Func...@>=
  231. void
  232. check_change() /* switches to |change_file| if the buffers match */
  233. {
  234.   int n=0; /* the number of discrepancies found */
  235.   if (lines_dont_match) return;
  236.   while (1) {
  237.     changing=1; change_line++;
  238.     if (!input_ln(change_file)) {
  239.       err_print("! Change file ended before @@y");
  240. @.Change file ended...@>
  241.       change_limit=change_buffer; changing=0;
  242.       return;
  243.     }
  244.     if (limit>buffer+1 && buffer[0]=='@@') {
  245.       if (isupper(buffer[1])) buffer[1]=tolower(buffer[1]);
  246.       @<If the current line starts with \.{@@y},
  247.         report any discrepancies and |return|@>;
  248.     }
  249.     @<Move |buffer| and |limit|...@>;
  250.     changing=0; cur_line++;
  251.     while (!input_ln(cur_file)) { /* pop the stack or quit */
  252.       if (include_depth==0) {
  253.         err_print("! CWEB file ended during a change");
  254. @.CWEB file ended...@>
  255.         input_has_ended=1; return;
  256.       }
  257.       include_depth--; cur_line++;
  258.     }
  259.     if (lines_dont_match) n++;
  260.   }
  261. }
  262.  
  263. @ @<If the current line starts with \.{@@y}...@>=
  264. if (buffer[1]=='x' || buffer[1]=='z') {
  265.   loc=buffer+2; err_print("! Where is the matching @@y?");
  266. @.Where is the match...@>
  267.   }
  268. else if (buffer[1]=='y') {
  269.   if (n>0) {
  270.     loc=buffer+2;
  271.     fprintf(stderr,"\n! Hmm... %d ",n);
  272.     err_print("of the preceding lines failed to match");
  273. @.Hmm... n of the preceding...@>
  274.   }
  275.   change_depth=include_depth;
  276.   return;
  277. }
  278.  
  279. @ The |reset_input| procedure gets the program ready to read the
  280. user's \.{WEB} input.
  281.  
  282. @<Func...@>=
  283. void
  284. reset_input()
  285. {
  286.   limit=buffer; loc=buffer+1; buffer[0]=' ';
  287.   @<Open input files@>;
  288.   include_depth=0; cur_line=0; change_line=0;
  289.   change_depth=include_depth;
  290.   changing=1; prime_the_change_buffer(); changing=!changing;
  291.   limit=buffer; loc=buffer+1; buffer[0]=' '; input_has_ended=0;
  292. }
  293.  
  294. @ The following code opens the input files.
  295. @^system dependencies@>
  296.  
  297. @<Open input files@>=
  298. if ((web_file=fopen(web_file_name,"r"))==NULL) {
  299.   strcpy(web_file_name,alt_web_file_name);
  300.   if ((web_file=fopen(web_file_name,"r"))==NULL)
  301.        fatal("! Cannot open input file ", web_file_name);
  302. }
  303. @.Cannot open input file@>
  304. @.Cannot open change file@>
  305. web_file_open=1;
  306. if ((change_file=fopen(change_file_name,"r"))==NULL)
  307.        fatal("! Cannot open change file ", change_file_name);
  308.  
  309. @ The |get_line| procedure is called when |loc>limit|; it puts the next
  310. line of merged input into the buffer and updates the other variables
  311. appropriately. A space is placed at the right end of the line.
  312. This procedure returns |!input_has_ended| because we often want to
  313. check the value of that variable after calling the procedure.
  314.  
  315. @<Fun...@>=
  316. get_line() /* inputs the next line */
  317. {
  318.   restart:
  319.   if (changing && include_depth==change_depth)
  320.    @<Read from |change_file| and maybe turn off |changing|@>;
  321.   if (! changing || include_depth>change_depth) {
  322.     @<Read from |cur_file| and maybe turn on |changing|@>;
  323.     if (changing && include_depth==change_depth) goto restart;
  324.   }
  325.   loc=buffer; *limit=' ';
  326.   if (*buffer=='@@' && (*(buffer+1)=='i' || *(buffer+1)=='I')) {
  327.     loc=buffer+2;
  328.     while (loc<=limit && (*loc==' '||*loc=='\t'||*loc=='"')) loc++;
  329.     if (loc>=limit) {
  330.       err_print("! Include file name not given");
  331. @.Include file name ...@>
  332.       goto restart;
  333.     }
  334.     if (include_depth>=max_include_depth-1) {
  335.       err_print("! Too many nested includes");
  336. @.Too many nested includes@>
  337.       goto restart;
  338.     } 
  339.     include_depth++; /* push input stack */
  340.     @<Try to open include file, abort push if unsuccessful, go to |restart|@>;
  341.   }
  342.   return (!input_has_ended);
  343. }
  344.  
  345. void put_line()
  346. {
  347.   char *ptr=buffer;
  348.   while (ptr<limit) putc(*ptr++,out_file);
  349.   putc('\n',out_file);
  350. }
  351.  
  352. @ When an \.{@@i} line is found in the |cur_file|, we must temporarily
  353. stop reading it and start reading from the named include file.  The
  354. \.{@@i} line should give a complete file name with or without
  355. double quotes.
  356. If the environment variable \.{CWEBINPUTS} is set, or if the compiler flag 
  357. of the same name was defined at compile time,
  358. \.{CWEB} will look for include files in the directory thus named, if
  359. it cannot find them in the current directory.
  360. (Colon-separated paths are not supported.)
  361. The remainder of the \.{@@i} line after the file name is ignored.
  362.  
  363. @d too_long() {include_depth--; 
  364.         err_print("! Include file name too long"); goto restart;}
  365.  
  366. @ @<Try to open...@>= {
  367.   char temp_file_name[max_file_name_length]; 
  368.   char *cur_file_name_end=cur_file_name+max_file_name_length-1; 
  369.   char *k=cur_file_name, *kk;
  370.   int l; /* length of file name */
  371.  
  372.   while (*loc!=' '&&*loc!='\t'&&*loc!='"'&&k<=cur_file_name_end) *k++=*loc++;
  373.   if (k>cur_file_name_end) too_long();
  374. @.Include file name ...@>
  375.   *k='\0';
  376.   if ((cur_file=fopen(cur_file_name,"r"))!=NULL) {
  377.     cur_line=0; 
  378.     goto restart; /* success */
  379.   }
  380.   kk=getenv("CWEBINPUTS");
  381.   if (kk!=NULL) {
  382.     if ((l=strlen(kk))>max_file_name_length-2) too_long();
  383.     strcpy(temp_file_name,kk);
  384.   }
  385.   else {
  386. #ifdef CWEBINPUTS
  387.     if ((l=strlen(CWEBINPUTS))>max_file_name_length-2) too_long();
  388.     strcpy(temp_file_name,CWEBINPUTS);
  389. #else
  390.     l=0; 
  391. #endif /* |CWEBINPUTS| */
  392.   }
  393.   if (l>0) {
  394.     if (k+l+2>=cur_file_name_end)  too_long();
  395. @.Include file name ...@>
  396.     for (; k>= cur_file_name; k--) *(k+l+1)=*k;
  397.     strcpy(cur_file_name,temp_file_name);
  398.     cur_file_name[l]='/'; /* \UNIX/ pathname separator */
  399.     if ((cur_file=fopen(cur_file_name,"r"))!=NULL) {
  400.       cur_line=0; 
  401.       goto restart; /* success */
  402.     }
  403.   }
  404.   include_depth--; err_print("! Cannot open include file"); goto restart;
  405. }
  406.  
  407. @ @<Read from |cur_file|...@>= {
  408.   cur_line++;
  409.   while (!input_ln(cur_file)) { /* pop the stack or quit */
  410.     if (include_depth==0) {input_has_ended=1; break;}
  411.     else {
  412.       fclose(cur_file); include_depth--;
  413.       if (changing && include_depth==change_depth) break;
  414.       cur_line++;
  415.     }
  416.   }
  417.   if (!changing && !input_has_ended)
  418.    if (limit-buffer==change_limit-change_buffer)
  419.     if (buffer[0]==change_buffer[0])
  420.       if (change_limit>change_buffer) check_change();
  421. }
  422.  
  423. @ @<Read from |change_file|...@>= {
  424.   change_line++;
  425.   if (!input_ln(change_file)) {
  426.     err_print("! Change file ended without @@z");
  427. @.Change file ended...@>
  428.     buffer[0]='@@'; buffer[1]='z'; limit=buffer+2;
  429.   }
  430.   if (limit>buffer) { /* check if the change has ended */
  431.     *limit=' ';
  432.     if (buffer[0]=='@@') {
  433.       if (isupper(buffer[1])) buffer[1]=tolower(buffer[1]);
  434.       if (buffer[1]=='x' || buffer[1]=='y') {
  435.         loc=buffer+2;
  436.         err_print("! Where is the matching @@z?");
  437. @.Where is the match...@>
  438.       }
  439.       else if (buffer[1]=='z') {
  440.         prime_the_change_buffer(); changing=!changing; 
  441.       }
  442.     }
  443.   }
  444. }
  445.  
  446. @ At the end of the program, we will tell the user if the change file
  447. had a line that didn't match any relevant line in |web_file|.
  448.  
  449. @<Funct...@>=
  450. void
  451. check_complete(){
  452.   if (change_limit!=change_buffer) { /* |changing| is 0 */
  453.     strncpy(buffer,change_buffer,change_limit-change_buffer+1);
  454.     limit=buffer+(int)(change_limit-change_buffer);
  455.     changing=1; change_depth=include_depth; loc=buffer;
  456.     err_print("! Change file entry did not match");
  457.   @.Change file entry did not match@>
  458.   }
  459. }
  460.  
  461. @* Reporting errors to the user.
  462. A global variable called |history| will contain one of four values
  463. at the end of every run: |spotless| means that no unusual messages were
  464. printed; |harmless_message| means that a message of possible interest
  465. was printed but no serious errors were detected; |error_message| means that
  466. at least one error was found; |fatal_message| means that the program
  467. terminated abnormally. The value of |history| does not influence the
  468. behavior of the program; it is simply computed for the convenience
  469. of systems that might want to use such information.
  470.  
  471. @d spotless 0 /* |history| value for normal jobs */
  472. @d harmless_message 1 /* |history| value when non-serious info was printed */
  473. @d error_message 2 /* |history| value when an error was noted */
  474. @d fatal_message 3 /* |history| value when we had to stop prematurely */
  475. @d mark_harmless {if (history==spotless) history=harmless_message;}
  476. @d mark_error history=error_message
  477.  
  478. @<Definit...@>=
  479. int history=spotless; /* indicates how bad this run was */
  480.  
  481. @ The command `|err_print("! Error message")|' will report a syntax error to
  482. the user, by printing the error message at the beginning of a new line and
  483. then giving an indication of where the error was spotted in the source file.
  484. Note that no period follows the error message, since the error routine
  485. will automatically supply a period. A newline is automatically supplied
  486. if the string begins with |"|"|.
  487.  
  488. The actual error indications are provided by a procedure called |error|.
  489.  
  490. @<Predecl...@>=
  491. void  err_print();
  492.  
  493. @
  494. @<Functions...@>=
  495. void
  496. err_print(s) /* prints `\..' and location of error message */
  497. char *s;
  498. {
  499.   char *k,*l; /* pointers into |buffer| */
  500.   fprintf(stderr,*s=='!'? "\n%s" : "%s",s);
  501.   if(web_file_open) @<Print error location based on input buffer@>;
  502.   update_terminal; mark_error;
  503. }
  504.  
  505. @ The error locations can be indicated by using the global variables
  506. |loc|, |cur_line|, |cur_file_name| and |changing|,
  507. which tell respectively the first
  508. unlooked-at position in |buffer|, the current line number, the current
  509. file, and whether the current line is from |change_file| or |cur_file|.
  510. This routine should be modified on systems whose standard text editor
  511. has special line-numbering conventions.
  512. @^system dependencies@>
  513.  
  514. @<Print error location based on input buffer@>=
  515. {if (changing && include_depth==change_depth)
  516.   printf(". (l. %d of change file)\n", change_line);
  517. else if (include_depth==0) fprintf(stderr,". (l. %d)\n", cur_line);
  518.   else fprintf(stderr,". (l. %d of include file %s)\n", cur_line, cur_file_name);
  519. l= (loc>=limit? limit: loc);
  520. if (l>buffer) {
  521.   for (k=buffer; k<l; k++)
  522.     if (*k=='\t') putc(' ',stderr);
  523.     else putc(*k,stderr); /* print the characters already read */
  524.   putchar('\n');
  525.   for (k=buffer; k<l; k++) putc(' ',stderr); /* space out the next line */
  526. }
  527. for (k=l; k<limit; k++) putc(*k,stderr); /* print the part not yet read */
  528.   putc('\n',stderr);
  529. }
  530.  
  531. @ When no recovery from some error has been provided, we have to wrap
  532. up and quit as graciously as possible.  This is done by calling the
  533. function |wrap_up| at the end of the code.
  534.  
  535. @d fatal(s,t) {
  536.   fprintf(stderr,s); err_print(t);
  537.   history=fatal_message; exit(wrap_up());
  538. }
  539.  
  540. @ Some implementations may wish to pass the |history| value to the
  541. operating system so that it can be used to govern whether or not other
  542. programs are started. Here, for instance, we pass the operating system
  543. a status of 0 if and only if only harmless messages were printed.
  544. @^system dependencies@>
  545.  
  546. @<Func...@>=
  547. wrap_up() {
  548.   @<Print the job |history|@>;
  549.   if (history > harmless_message) return(1);
  550.   else return(0);
  551. }
  552.  
  553. @ @<Print the job |history|@>=
  554. switch (history) {
  555. case spotless: if (show_happiness) fprintf(stderr,"(No errors were found.)\n"); break;
  556. case harmless_message:
  557.   fprintf(stderr,"(Did you see the warning message above?)\n"); break;
  558. case error_message:
  559.   fprintf(stderr,"(Pardon me, but I think I spotted something wrong.)\n"); break;
  560. case fatal_message: fprintf(stderr,"(That was a fatal error, my friend.)\n");
  561. } /* there are no other cases */
  562.  
  563. @* Command line arguments.
  564. The user calls \.{wmerge} with arguments on the command line.
  565. These are either file names or flags to be turned off (beginning with |"-"|)
  566. or flags to be turned on (beginning with |"+"|.
  567. The following globals are for communicating the user's desires to the rest
  568. of the program. The various file name variables contain strings with
  569. the names of those files. Most of the 128 flags are undefined but available
  570. for future extensions.
  571.  
  572. @d show_banner flags['b'] /* should the banner line be printed? */
  573. @d show_happiness flags['h'] /* should lack of errors be announced? */
  574.  
  575. @<Defin...@>=
  576. int argc; /* copy of |ac| parameter to |main| */
  577. char **argv; /* copy of |av| parameter to |main| */
  578. char out_file_name[max_file_name_length]; /* name of |out_file| */
  579. boolean flags[128]; /* an option for each 7-bit code */
  580.  
  581. @ The |flags| will be initially 1.
  582.  
  583. @<Set the default options@>=
  584. show_banner=show_happiness=1;
  585.  
  586. @ We now must look at the command line arguments and set the file names
  587. accordingly.  At least one file name must be present: the \.{WEB}
  588. file.  It may have an extension, or it may omit it to get |'.w'|
  589. added.
  590.  
  591. If there is another file name present among the arguments, it is the
  592. change file, again either with an extension or without one to get |'.ch'|
  593. An omitted change file argument means that |'/dev/null'| should be used,
  594. when no changes are desired.
  595. @^system dependencies@>
  596.  
  597. If there's a third file name, it will be the output file.
  598.  
  599. @<Pred...@>=
  600. void scan_args();
  601.  
  602. @
  603. @<Function...@>=
  604. void
  605. scan_args()
  606. {
  607.   char *dot_pos; /* position of |'.'| in the argument */
  608.   char *name_pos; /* file name beginning, sans directory */
  609.   register char *s; /* register for scanning strings */
  610.   boolean found_web=0,found_change=0,found_out=0;
  611.              /* have these names have been seen? */
  612.   boolean flag_change;
  613.  
  614.   while (--argc > 0) {
  615.     if (**(++argv)=='-' || **argv=='+') @<Handle flag argument@>@;
  616.     else {
  617.       s=name_pos=*argv;@+dot_pos=NULL;
  618.       while (*s) {
  619.         if (*s=='.') dot_pos=s++;
  620.         else if (*s=='/') dot_pos=NULL,name_pos=++s;
  621.         else s++;
  622.       }
  623.       if (!found_web) @<Make |web_file_name|@>@;
  624.       else if (!found_change) @<Make |change_file_name| from |fname|@>@;
  625.       else if (!found_out) @<Override output file name@>@;
  626.         else @<Print usage error message and quit@>;
  627.     }
  628.   }
  629.   if (!found_web) @<Print usage error message and quit@>;
  630.   if (!found_change) strcpy(change_file_name,"/dev/null");
  631. }
  632.  
  633. @ We use all of |*argv| for the |web_file_name| if there is a |'.'| in it,
  634. otherwise we add |".w"|. If this file can't be opened, we prepare an
  635. |alt_web_file_name| by adding |"web"| after the dot.
  636. The other file names come from adding other things
  637. after the dot.  We must check that there is enough room in
  638. |web_file_name| and the other arrays for the argument.
  639.  
  640. @<Make |web_file_name|...@>=
  641. {
  642.   if (s-*argv > max_file_name_length-5)
  643.     @<Complain about argument length@>;
  644.   if (dot_pos==NULL)
  645.     sprintf(web_file_name,"%s.w",*argv);
  646.   else {
  647.     strcpy(web_file_name,*argv);
  648.     *dot_pos=0; /* string now ends where the dot was */
  649.   }
  650.   sprintf(alt_web_file_name,"%s.web",*argv);
  651.   *out_file_name='\0'; /* this will print to stdout */
  652.   found_web=1;
  653. }
  654.  
  655. @ @<Make |change_file_name|...@>=
  656. {
  657.   if (s-*argv > max_file_name_length-4)
  658.     @<Complain about argument length@>;
  659.   if (dot_pos==NULL)
  660.     sprintf(change_file_name,"%s.ch",*argv);
  661.   else strcpy(change_file_name,*argv);
  662.   found_change=1;
  663. }
  664.  
  665. @ @<Override...@>=
  666. {
  667.   if (s-*argv > max_file_name_length-5)
  668.     @<Complain about argument length@>;
  669.   if (dot_pos==NULL) sprintf(out_file_name,"%s.out",*argv);
  670.   else strcpy(out_file_name,*argv);
  671.   found_out=1;
  672. }
  673.  
  674. @ @<Handle flag...@>=
  675. {
  676.   if (**argv=='-') flag_change=0;
  677.   else flag_change=1;
  678.   for(dot_pos=*argv+1;*dot_pos>'\0';dot_pos++)
  679.     flags[*dot_pos]=flag_change;
  680. }
  681.  
  682. @ @<Print usage error message and quit@>=
  683. {
  684.   fatal("! Usage: wmerge webfile[.w] [changefile[.ch] [outfile[.out]]]\n","")@;
  685. }
  686.  
  687. @ @<Complain about arg...@>= fatal("! Filename too long\n", *argv);
  688.  
  689. @* Output. Here is the code that opens the output file:
  690. @^system dependencies@>
  691.  
  692. @<Defin...@>=
  693. FILE *out_file; /* where output goes */
  694.  
  695. @ @<Scan arguments and open output file@>=
  696. scan_args();
  697. if (out_file_name[0]=='\0') out_file=stdout;
  698. else if ((out_file=fopen(out_file_name,"w"))==NULL)
  699.     fatal("! Cannot open output file ", out_file_name);
  700. @.Cannot open output file@>
  701.  
  702. @ The |update_terminal| procedure is called when we want
  703. to make sure that everything we have output to the terminal so far has
  704. actually left the computer's internal buffers and been sent.
  705. @^system dependencies@>
  706.  
  707. @d update_terminal fflush(stderr) /* empty the terminal output buffer */
  708.  
  709. @* Index.
  710.